package com.runa.rccprotocol.analysis.pusai;

import com.alibaba.fastjson.JSON;
import com.runa.monitor.comm.dto.newComm.ValveVO;
import com.runa.protocol.dto.IInterData;
import com.runa.protocol.enums.ByteOrder;
import com.runa.protocol.util.ByteUtil;
import com.runa.rccprotocol.dto.runa.RunaConcenAffirmDeny;
import com.runa.rccprotocol.dto.runa.RunaConcenResponseData;
import lombok.Data;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 2024/12/3 11:48
 * zhangYB
 */
@Data
public class PsAnalysis {

    private IInterData interData;

    public int valveHandle(byte[] data, int begin, int length) {
        if (begin + 11 > length) {
            return -1;
        }
        boolean end = true;
        for (int i = begin; i < length; i++) {
            if ((data[i] & 0xff) == 0x68 && (data[i+1] & 0xff) == 0x41 && (data[i+9] & 0xff) == 0x81) {
                begin = i;
                end = false;
            }
        }
        if (end) {
            return 0;
        }

        int index = begin + 2;
        String serial = ByteUtil.byteToHexStringFlip(data, index, 6);
        index += 7;
        index += 1;
        int l = data[index++] & 0xff;

        if (begin + 11 + l + 2 > length) {
            return -1;
        }

        if (data[begin + 11 + l] != ByteUtil.sum(data, begin, 11 + l)) {
            return 0;
        }

        if (((data[index] & 0xff) == 0x2D && (data[index+1] & 0xff) == 0x90)
                || ((data[index] & 0xff) == 0x90 && (data[index+1] & 0xff) == 0x2D)) {
            readValveResponseAnalysis(data, index+3, serial);
        } else if ((data[index] & 0xff) == 0x17 && (data[index+1] & 0xff) == 0xA0) {
            this.interData = new RunaConcenAffirmDeny(true);
        }

        return 0;
    }

    private void readValveResponseAnalysis(byte[] data, int index, String serial) {
        ValveVO valveVO = new ValveVO();
        valveVO.setValveSerial(serial);
        valveVO.setReadTime(new Date());
        valveVO.setOpenTime((float)ByteUtil.byteToUInt(data, index, ByteOrder.DCBA));
        index += 4;
        // 累计热量 4
        index += 5;
        // 面板/室内温度1 3
        index += 3;
        // 室内温度2 3
        index += 3;
        // 室内温度3 3
        index += 3;
        // 室内温度4 3
        index += 3;
        // 室内温度5 3
        index += 3;
        // 设置温度 3
        index += 3;
        // 状态 2
        valveVO.setStatus(ByteUtil.byteToHexStringFlip(data, index, 2));
        byte s1 = data[index++];
        byte s2 = data[index++];
        List<String> faultList = new ArrayList<>();
        if ((s1 & 0b11) == 0b00) {
            // 开阀
            valveVO.setActOpen(100f);
        } else if ((s1 & 0b11) == 0b01) {
            // 关阀
            valveVO.setActOpen(0f);
        } else if ((s1 & 0b11) == 0b10) {
            // 开度控制
            valveVO.setActOpen((float)(data[index] & 0xff));
        } else if ((s1 & 0b11) == 0b11) {
            // 异常
            faultList.add("阀门异常");
        }
        index += 6;
        valveVO.setRemoteSet((float)(data[index] & 0xff));

        RunaConcenResponseData runaConcenResponseData = new RunaConcenResponseData();
        runaConcenResponseData.addRepData(valveVO);
        this.interData = runaConcenResponseData;
    }

    public static void main(String[] args) {
        // 抄阀成功返回
//        String s = "68 41 06 05 04 03 02 01 10 81 2E 90 2D FF 6B 16 00 00 00 00 00 00 05 10 30 00 00 90 00 00 90 00 00 90 00 00 90 00 00 25 00 01 40 00 00 00 00 00 0D 28 00 00 00    00    00    5A 00 34 16";
        // 控阀成功返回
        String s = "684165971100242010812E902DEF53EE600000000000050090000090000090000090000090000037000A01684165971100242010810517A00000084F16";
        byte[] data = ByteUtil.hexStringToByte(s);
        PsAnalysis psAnalysis = new PsAnalysis();
        psAnalysis.valveHandle(data, 0, data.length);
        System.out.println(JSON.toJSONString(psAnalysis.getInterData()));
    }

}
